import { Pair as M } from '../nodes/Pair.js';
import { YAMLMap as P } from '../nodes/YAMLMap.js';
import { resolveProps as T } from './resolve-props.js';
import { containsNewline as S } from './util-contains-newline.js';
import { flowIndentCheck as A } from './util-flow-indent-check.js';
import { mapIncludes as K } from './util-map-includes.js';
const g = 'All mapping items must start at the same column';
function D({ composeNode: y, composeEmptyNode: _ }, t, l, e, k) {
  var N;
  const L = (k == null ? void 0 : k.nodeClass) ?? P,
    a = new L(t.schema);
  t.atRoot && (t.atRoot = !1);
  let s = l.offset,
    p = null;
  for (const I of l.items) {
    const { start: u, key: i, sep: f, value: c } = I,
      n = T(u, {
        indicator: 'explicit-key-ind',
        next: i ?? (f == null ? void 0 : f[0]),
        offset: s,
        onError: e,
        startOnNewline: !0,
      }),
      h = !n.found;
    if (h) {
      if (
        (i &&
          (i.type === 'block-seq'
            ? e(
                s,
                'BLOCK_AS_IMPLICIT_KEY',
                'A block sequence may not be used as an implicit map key',
              )
            : 'indent' in i && i.indent !== l.indent && e(s, 'BAD_INDENT', g)),
        !n.anchor && !n.tag && !f)
      ) {
        (p = n.end),
          n.comment &&
            (a.comment
              ? (a.comment +=
                  `
` + n.comment)
              : (a.comment = n.comment));
        continue;
      }
      (n.hasNewlineAfterProp || S(i)) &&
        e(
          i ?? u[u.length - 1],
          'MULTILINE_IMPLICIT_KEY',
          'Implicit keys need to be on a single line',
        );
    } else
      ((N = n.found) == null ? void 0 : N.indent) !== l.indent &&
        e(s, 'BAD_INDENT', g);
    const w = n.end,
      o = i ? y(t, i, n, e) : _(t, w, u, null, n, e);
    t.schema.compat && A(l.indent, i, e),
      K(t, a.items, o) && e(w, 'DUPLICATE_KEY', 'Map keys must be unique');
    const m = T(f ?? [], {
      indicator: 'map-value-ind',
      next: c,
      offset: o.range[2],
      onError: e,
      startOnNewline: !i || i.type === 'block-scalar',
    });
    if (((s = m.end), m.found)) {
      h &&
        ((c == null ? void 0 : c.type) === 'block-map' &&
          !m.hasNewline &&
          e(
            s,
            'BLOCK_AS_IMPLICIT_KEY',
            'Nested mappings are not allowed in compact mappings',
          ),
        t.options.strict &&
          n.start < m.found.offset - 1024 &&
          e(
            o.range,
            'KEY_OVER_1024_CHARS',
            'The : indicator must be at most 1024 chars after the start of an implicit block mapping key',
          ));
      const d = c ? y(t, c, m, e) : _(t, s, f, null, m, e);
      t.schema.compat && A(l.indent, c, e), (s = d.range[2]);
      const C = new M(o, d);
      t.options.keepSourceTokens && (C.srcToken = I), a.items.push(C);
    } else {
      h &&
        e(
          o.range,
          'MISSING_CHAR',
          'Implicit map keys need to be followed by map values',
        ),
        m.comment &&
          (o.comment
            ? (o.comment +=
                `
` + m.comment)
            : (o.comment = m.comment));
      const d = new M(o);
      t.options.keepSourceTokens && (d.srcToken = I), a.items.push(d);
    }
  }
  return (
    p && p < s && e(p, 'IMPOSSIBLE', 'Map comment with trailing content'),
    (a.range = [l.offset, s, p ?? s]),
    a
  );
}
export { D as resolveBlockMap };
